<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/24 0024
 * Time: 上午 6:15
 */

namespace App\Model\Facades;


use App\Model\Entity\AdminMenu;

class AdminMenus extends Common
{
    static private $_interface = null;
    private $treeList = null;

    public function __construct()
    {
        $this->treeList = AdminMenu::all();
    }

    static public function create()
    {
        if (empty(self::$_interface)) {
            return new static();
        }
        return self::$_interface;
    }

    public function add()
    {
        $data = request()->get('data');

        list($errors, $v) = $this->dataValidator($data, [
            'name' => 'required'
        ], [
            'name.required' => '菜单名称不能为空'
        ]);

        if (!empty($errors))
            return [$errors, $v];

        $roles = new AdminMenu();
        $roles->fill($data);
        return [$roles->save(), []];

    }

    public function edit($id)
    {
        $data = request()->get('data');

        list($errors, $v) = $this->dataValidator($data, [
            'name' => 'required'
        ], [
            'name.required' => '菜单名称不能为空'
        ]);

        if (!empty($errors))
            return [$errors, $v];

        $roles = $this->show($id);

        $roles->fill($data);
        return [$roles->save(), []];

    }

    public function show($id)
    {
        return AdminMenu::find($id);
    }

    public function index()
    {

    }

    public function delete()
    {
        parent::delete(); // TODO: Change the autogenerated stub
    }

    public function getInfoForAlias($alias)
    {
        return AdminMenu::where('sulg', $alias)->first();
    }

    public function getToTree($id = 'id', $parent_id = 'parent_id', $pid = 0, $laraevl = 0)
    {
        $arr = [];
        foreach ($this->treeList->toArray() as $key => $value) {
            if ($value[$parent_id] == $pid) {
                $arr[$key] = $value;
                $arr[$key]['laraevl'] = $laraevl;
                $arr[$key]['son'] = self::getToTree($id, $parent_id, $value['id'], $laraevl + 1);
            }
        }
        return collect($arr);
    }

    public function getToTreeOption($id = 'id', $parent_id = 'parent_id', $pid = 0, $laraevl = 0)
    {
        $arr = [];
        foreach ($this->treeList->toArray() as $key => $value) {
            if ($value[$parent_id] == $pid) {
                $value['laraevl'] = $laraevl;
                $arr[] = $value;
                $arr = array_merge($arr, self::getToTreeHtml($id, $parent_id, $value['id'], $laraevl + 1));
            }
        }
        return $arr;
    }

    public function getToTreeHtml($data = [])
    {
        $arr = $this->getToTree();
        $html = "";
        foreach ($arr as $row) {
            $checked = in_array($row['id'], $data) ? 'checked' : "";

            $html .= "<div class='col-md-4 _menusBox' style='min-height: 200px;'>";
            $html .= "<div style='padding: 5px 15px'><h3 class='text-danger'>
                        <input type='checkbox' name='data[muen][]' value='{$row["id"]}' {$checked} class='menuall'>
                       {$row['name']}</h3></div>";

            $html .= $this->hasTreeSon($row['son'], $data);

            $html .= "</div>";
        }

        return $html;
    }

    public function hasTreeSon($data, $check_array)
    {

        $result = "";
        $result .= " <ul>";
        foreach ($data as $row) {
            $checked = in_array($row['id'], $check_array) ? 'checked' : "";
            $result .= " <li><input type='checkbox' name='data[muen][]' value='{$row["id"]}' {$checked} class='menucheck'>{$row['name']}</li> ";
            if (!($row['son']->isEmpty()))
                $result .= self::hasTreeSon($row['son'], $check_array);
        }
        $result .= "</ul> ";
        return $result;
    }

}




















